﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp;
using EmbeddedSharp.Core;
using EmbeddedSharp.Core.Function;

namespace EmbeddedSharp.Scripting.Function.Debugging {

    /// <summary>
    /// Assert the pair is the same
    /// </summary>
    public class AssertNode : FuncNode {

        public AssertNode()
            : base() {
            base.Value = "assert";
        }

        public override object Evaluate() {
            if (Parameters.Count < 2) {
                return "usage: assert(expected, actual[, assertionMessage])";
            }

            object expected = Parameters[0].Evaluate();
            object actual = Parameters[1].Evaluate();

            if (expected == null) {
                throw new AssertException("Expected is null.");
            }
            else if(actual == null) {
                throw new AssertException("Acutal is null.");
            } else if(expected.ToString() != actual.ToString()) {
                if (Parameters.Count == 3) {
                    string assertionMessage = (string)Parameters[2].Evaluate();
                    throw new AssertException(assertionMessage);
                } else {
                    throw new AssertException(string.Format("{0} is not as expected as {1}", actual, expected));
                }
            }

            return true;
        }
    }
}
